For an example, look in the Save command button for the following code:
Private Sub cmdSave_Click()
If txtLast.Text = "" Then
MsgBox "Last Name Must Be Filled In"
End If
End Sub
The Else Clause
The If statement also allows you to add an Else clause you can use to execute a different set of code if the condition evaluates to a False condition. Heres an example:
Private Sub cmdSave_Click()
If txtLast.Text = "" Then
MsgBox "Last Name Must Be Filled In"
End If If optMale.Value = True Then MsgBox "Employee is Male" Else MsgBox "Employee is Female"
End If
End Sub
In this example, the code between Else and End If is executed if optMale.Value is set to False.
The ElseIf Clause
If you have multiple conditions to check, you can also use the ElseIf clause. Each condition is checked until a True condition is found. Once the True condition is found, the code within the block between the ElseIf and the next ElseIf or End If statement is executed. After executing the block of code, the next line executed will be after the End If statement.
Heres the syntax:
If <Cond1> Then
<Statement Block 1>
[ElseIf] <Cond2> Then
<Statement Block 2>
[Else]
<Statement Block 3>
End If